﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FisheryPlatform.Entity;
using FisheryPlatform.Bussiness.LoginCookie;
using FisheryPlatform.Bussiness.Post;
using FisheryPlatform.Bussiness;
using FisheryPlatform.Bussiness.Option;

namespace FisheryPlatform.Web.Admin.Posts
{
    public partial class New : System.Web.UI.Page
    {
        protected int flag = -1;
        protected PostMixEntity postMixInfo = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            if (IsPostBack)
            {
                Save();
            }
            else
            {
                InitData();
            }
            #endregion
        }

        private void InitData()
        {
            #region
            long postId = 0;
            long.TryParse(Request.QueryString["postId"], out postId);
            postMixInfo = PostManager.GetByPostId(postId);
            txt_Title.Value = postMixInfo.Post.Title;
            hEditor_Content.Html = postMixInfo.Post.Content;
            sel_Status.Value = ((int)postMixInfo.Post.Status).ToString();

            var optionNameList = OptionNameConstants.List;
            OptionsEntity optionInfo = null;
            foreach (var optionName in optionNameList)
            {
                optionInfo = OptionManager.GetByOptionName(optionName);
                if (string.IsNullOrEmpty(optionInfo.Name) == false
                    && optionInfo.Value.Split(',').Contains<string>(postId.ToString()))
                {
                    txt_HotSettings.Value += optionName + ",";
                }
            }
            txt_HotSettings.Value = txt_HotSettings.Value.Trim(',');
            #endregion
        }

        private void Save()
        {
            #region
            try
            {
                if (string.IsNullOrEmpty(txt_Title.Value) || string.IsNullOrEmpty(hEditor_Content.Text))
                {
                    flag = 2;
                    return;
                }
                postMixInfo = new PostMixEntity();
                long postId = 0;
                long.TryParse(Request.QueryString["postId"], out postId);
                postMixInfo.Post.PostId = postId;
                postMixInfo.Post.Title = txt_Title.Value.Trim();
                postMixInfo.Post.Content = hEditor_Content.Html.Trim();
                postMixInfo.Post.AuthorId = new LoginUserInfo().UserID;
                postMixInfo.Post.Type = PostTypeEnum.Post;
                postMixInfo.Post.Status = (PostStatusEnum)(int.Parse(sel_Status.Value));
                postMixInfo.Post.PostDate = DateTime.MaxValue;
                postMixInfo.Post.ModifyDate = DateTime.MaxValue;
                foreach (var cateId in txt_Cates.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    postMixInfo.Terms.Add(new TermEntity { TermId = long.Parse(cateId), Taxonomy = TermTaxonomyEnum.Cate });
                }
                foreach (var tagId in txt_Tags.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    postMixInfo.Terms.Add(new TermEntity { TermId = long.Parse(tagId), Taxonomy = TermTaxonomyEnum.Tag });
                }

                if (PostManager.Save(postMixInfo))
                {
                    UpdateHotSettings(postMixInfo.Post.PostId.ToString());
                    flag = 1;
                }
                else
                {
                    flag = 0;
                }
            }
            catch (Exception ex)
            {
                ExceptionMessage.Record(ex);
            }
            #endregion
        }

        private void UpdateHotSettings(string postId)
        {
            #region
            var selectedList = txt_HotSettings.Value.Split(',');
            var optionNameList = OptionNameConstants.List;
            foreach (var optionName in optionNameList)
            {
                OptionManager.Save(optionName, postId, selectedList.Contains<string>(optionName));
            }
            #endregion
        }

        protected string GetCheckedStatus(long termId)
        {
            #region
            foreach (var item in postMixInfo.Terms)
            {
                if (item.TermId == termId)
                {
                    return "checked='true'";
                }
            }
            return "";
            #endregion
        }
    }
}